#include "bsp_ap3216c.h"
#include "bsp_i2c.h"
#include "bsp_delay.h"
#include "stdio.h"
static uint8_t ap3216_write(uint8_t reg, uint8_t data);
static uint8_t ap3216_read(uint8_t reg);
#define ADDR 0X1E
void ap3216_init(void)
{
    IOMUXC_SetPinMux(IOMUXC_UART4_TX_DATA_I2C1_SCL, 1);
    IOMUXC_SetPinMux(IOMUXC_UART4_RX_DATA_I2C1_SDA, 1);

    IOMUXC_SetPinConfig(IOMUXC_UART4_TX_DATA_I2C1_SCL, 0x70b0);
    IOMUXC_SetPinConfig(IOMUXC_UART4_RX_DATA_I2C1_SDA, 0x70b0);
    printf("i2c_init\r\n");
    i2c_init(I2C1);
    printf("i2c_init1\r\n");
    ap3216_write(AP3216C_SYSTEMCONFIG, 0x4); // 复位
    printf("i2c_init2\r\n");
    delayms(50);
    ap3216_write(AP3216C_SYSTEMCONFIG, 0x3); // open
    printf("i2c_init3\r\n");
    int val = ap3216_read(AP3216C_SYSTEMCONFIG); 
    printf("val = %x \r\n", val);
}
// 读一个字节
static uint8_t ap3216_read(uint8_t reg)
{
    uint8_t val;
    struct i2c_transfer trans;
    trans.direction = kI2C_Read;
    trans.slaveAddress = ADDR;
    trans.subaddress = reg;
    trans.subaddressSize = 1;
    trans.data = &val;
    trans.dataSize = 1;

    i2c_master_transfer(I2C1, &trans);
    return val;
} 
static uint8_t ap3216_write(uint8_t reg, uint8_t data)
{
    struct i2c_transfer trans;
    trans.direction = kI2C_Write;
    trans.slaveAddress = ADDR;
    trans.subaddress = reg;
    trans.subaddressSize = 1;
    trans.data = &data;
    trans.dataSize = 1;
    return i2c_master_transfer(I2C1, &trans);
} 

